package 剑指offer_leetcode;


import java.util.ArrayList;

/**
 * JZ6从尾到头打印链表
 */

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public class 剑指Offer06_从尾到头打印链表 {
    public int[] reversePrint(ListNode head) {
        ArrayList<Integer> res = new ArrayList<>();
        reverse(head,res);
        if(res.size()==0)
            return new int[0];
        else{
            int[] arr = new int[res.size()];
            for(int i=0;i<res.size();i++)
                arr[i] = res.get(i);
            return arr;
        }
    }

    void reverse(ListNode head, ArrayList list){
        if(head!=null){
            reverse(head.next,list);
            list.add(head.val);
        }
    }
}
